
#include "ShortCommand.h"


void * threadFunc(void * arg){
    PthreadPool * pp = (PthreadPool *)arg;
    printf("I'm ready(short command)!\n");
    while(1){

        pthread_mutex_lock(&pp->lock1);
        while(pp->rdy_que.size == 0){
            pthread_cond_wait(&pp->cond,&pp->lock1);
        }
        int netfd = deleteQueueNode(&pp->rdy_que);
        MYSQL * mysql = mysql_init(NULL);
        MYSQL * ret = mysql_real_connect(mysql,"localhost","root","123","netdisk",0,NULL,0);
        pthread_mutex_unlock(&pp->lock1);
        if(ret == NULL){
            fprintf(stderr,"mysql_real_connect:%s\n",mysql_error(mysql));
            mysql_close(mysql);
            printf("线程退出再监听\n");
            continue;
        }

        Train train;
        int epollfd = epoll_create(1);
        addEpollNode(epollfd,netfd);
        struct epoll_event readyset[1024];


        Usr usr;


        while(1){
            bzero(readyset,sizeof(readyset));
            int length;
            char c;//用于判断是注册还是登录y/n
            char signal;//用于给客户端发信号，判断是否成功
            epoll_wait(epollfd,readyset,1024,-1);
            int ret = recv(netfd,&c,1,MSG_WAITALL);
            if(ret == 0){
                break;
            }
            if(c == 'y'){//进入注册语句
                while(1){
                    printf("进入注册!\n");
                    bzero(&usr,sizeof(usr));
                    epoll_wait(epollfd,readyset,1024,-1);
                    recv(netfd,&length,sizeof(int),MSG_WAITALL);
                    int sret = recv(netfd,&usr,length,MSG_WAITALL);
                    if(sret == 0){
                        break;
                    }
                    int ret = registered_server(usr.usrname,usr.password,mysql);
                    if(ret == 0){
                        signal = 'y';
                        send(netfd,&signal,1,MSG_NOSIGNAL);
                        break;
                    }
                    else{
                        signal = 'n';
                        send(netfd,&signal,1,MSG_NOSIGNAL);
                        break;
                    }
                }
            }
            else if(c == 'n'){//进入登录语句
                int ret;
                while(1){
                    printf("进入登录\n");
                    bzero(&usr,sizeof(usr));
                    epoll_wait(epollfd,readyset,1024,-1);
                    int ret1 = recv(netfd,&length,sizeof(int),MSG_WAITALL);
                    if(ret1 == 0){
                        break;
                    }
                    int ret2 = recv(netfd,&usr,length,MSG_WAITALL);
                    if(ret2 == 0){
                        break;
                    }
                    ret = littleLogin(usr.usrname,usr.password,mysql);
                    if(ret == 0){
                        signal = 'y';
                        send(netfd,&signal,1,MSG_NOSIGNAL);
                        goto end;//登录成功则跳出循环
                    }
                    else{
                        signal = 'n';
                        send(netfd,&signal,1,MSG_NOSIGNAL);
                    }
                }
            }
        }
end:


        printf("%s\n",usr.usrname);
        char * username = (char *)calloc(128,sizeof(char));
        memcpy(username,usr.usrname,strlen(usr.usrname));


        while(1){
            epoll_wait(epollfd,readyset,1024,-1);
            int rret = recv(readyset[0].data.fd,&train.length,sizeof(train.length),MSG_WAITALL);
            if(rret == 0){
                printf("客户端断开连接\n");
                break;
            }
            recv(readyset[0].data.fd,train.data,train.length,MSG_WAITALL);
            char command[20] = {0};
            memcpy(command,train.data,train.length);
            printf("command:%s\n",command);
            
            if(strcmp(command,"puts") == 0 || strcmp(command,"gets") == 0){
                char * JWT =  encode();
                printf("JWT:%s\n",JWT);
                char ip[] = "192.168.220.128";
                char port[] = "9999";
                sendT(netfd,JWT);
                sendT(netfd,ip);
                sendT(netfd,port);

                continue;
            }
                

            char prev_path[1024] = {0};        // 父文件的文件路径（对应表项就是path)
            recv(readyset[0].data.fd,&train.length,sizeof(train.length),MSG_WAITALL);
            recv(readyset[0].data.fd,train.data,train.length,MSG_WAITALL);
            memcpy(prev_path,train.data,train.length);
            printf("prev_path:%s\n",prev_path);


            char real_path[] = "../../../netdisk";
            train.length = 0;
            bzero(train.data,sizeof(train.data));
            if(strcmp(command,"cd") == 0){
                cdDispose(netfd,mysql,username,prev_path);
            }
            if(strcmp(command,"ls") == 0){
                lsDispose(readyset[0].data.fd,mysql,username,prev_path);
            }
            if(strcmp(command,"mkdir") == 0){
                printf("intomkdirDispose\n");
                mkdirDispose(readyset[0].data.fd,mysql,username,prev_path);
                printf("outmkdirDispose\n");
            }
            if(strcmp(command,"rmdir") == 0){
                printf("intormdirDispose\n");
                rmdirDispose(readyset[0].data.fd,mysql,username,prev_path);
                printf("outrmdirDispose\n");
            }
            if(strcmp(command,"remove") == 0){
                printf("intoremoveDispose\n");
                rmfileDispose(readyset[0].data.fd,mysql,username,prev_path,real_path);
                printf("outremoveDispose\n");
            }
        }
        printf("线程退出再监听\n");
    }

    //  transfile(netfd);

    pthread_exit(NULL);
}

